package com.dduggs.note.javaMail;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtils2 {

    public static void sendMail(String sendTo, String code) throws MessagingException {

        //创建连接对象，连接到邮件服务器
        Properties properties = new Properties();
//        properties.setProperty("host","");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xxx@xxx.com", "123456");
            }
        });
        //创建邮件对象
        Message message =new MimeMessage(session);
        //设置发件人
        Address address=new InternetAddress("xxx@xxx.com");
        message.setFrom(address);
        //设置收件人
        message.setRecipient(Message.RecipientType.TO,new InternetAddress(sendTo));
        //设置主题
        message.setSubject("来自xxx的激活邮件");
        //设置正文
        message.setContent("<h1>来自xx网站的激活邮件，激活请点击：</h1><h3><a href='http://localhost:8080/register/ActiveServlet?code="+code+"'>http://localhost:8080/register/ActiveServlet?code="+code+"</a></h3>","text/html;charset=UTF-8");
        //发送邮件
        Transport.send(message);
    }
}
